//
//  V4BrowseLogViewController.m
//  XinKaiFa55like
//
//  Created by 55like on 25/09/2017.
//  Copyright © 2017 55like lj. All rights reserved.
//

#import "V4BrowseLogViewController.h"
#import "RHTableView.h"
#import "V4TestLicensePlateContentViewController.h"
#import "NavigationBarToggleTabView.h"
#import "MyUiIAlertView.h"
#import "NewsTableViewCell.h"
#import "InsuranceBrokersContentViewController.h"
@interface V4BrowseLogViewController ()<RHTableViewDelegate>
{
    
    //---------------v4--------------
    NavigationBarToggleTabView *toggleTab;
    
    
}
@property(nonatomic,strong)RHTableView*mtableView;
@property(nonatomic,copy)NSString*successStr;
//@property(nonatomic,strong)RHTableView*mtableView2;
@property(nonatomic,copy)NSString*type;

@end

@implementation V4BrowseLogViewController
#pragma mark  开始
- (void)viewDidLoad {
    [super viewDidLoad];
    [self navbarTitle:@" "];
    
    [self addView];
    [self initNavTopButtons];
    
    [self rightButton:nil image:@"xkf_delbs" sel:@selector(rightBtnClick:)];
    
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self request];

}
-(void)rightBtnClick:(UIButton*)btn{
    
    NSString*titlestr=[self.type isEqualToString:@"products"]?@"是否删除牌照浏览记录？":@"是否删除新闻浏览记录？";
    [MyUiIAlertView showAlerttWithTitle:titlestr message:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定" WithAlertBlock:^(UIAlertView * _Nonnull alertView, NSInteger buttonIndex) {
        if (buttonIndex==1) {
            krequestParam
            if (_type) {
                [dictparam setObject:_type forKey:@"type"];
            }
            [NetEngine createPostAction:@"user/delbrowse" withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
                if ([[resData getSafeObjWithkey:@"status"] isEqualToString:@"200"]) {
//                    NSDictionary *dic=[resData getSafeObjWithkey:@"data"];
                    
                    [self request];
                    _successStr=[resData valueForJSONKey:@"info"];
                    //            [self addView];
                    
                }else{
                    [SVProgressHUD showErrorWithStatus:[resData valueForJSONKey:@"info"]];
                    
                }
            }];
            

        }
    }];
    
    
}
#pragma mark -  写UI
-(void)addView{
    _mtableView =[[RHTableView alloc]initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, kScreenHeight-kTopHeight-49) style:UITableViewStylePlain];
    
    _mtableView.delegate = self;
    _mtableView.dataSource = self;
    _mtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _mtableView.delegate2=self;
//    [_mtableView registerClass:[NewsTableViewCell class] forCellReuseIdentifier:@"NewsTableViewCell"];
    
    [self.view addSubview:_mtableView];
    
    
    
    
}

-(void)refreshData:(RHTableView *)view{
    if(_successStr){
        [SVProgressHUD showSuccessWithStatus:_successStr];
        _successStr=nil;
    }

}
-(void)initNavTopButtons{
    toggleTab=[[NavigationBarToggleTabView alloc] initWithFrame:CGRectMake((kScreenWidth-150)/2, 28, 150, 28)];
    [self.navView addSubview:toggleTab];
    
    [self refreshNavTopButtonsData];
}
-(void)refreshNavTopButtonsData{
      __weak __typeof(self) weakSelf = self;
    __weak RHTableView *weakTable1=_mtableView;
//    __weak RHTableView *weakTable2=_mtableView2;
    [toggleTab setTiltelArray:@[@{@"title":@"牌照产品",@"id":@"products"},@{@"title":@"新闻快讯",@"id":@"news"}] toggleTab:^(NSDictionary *dicSelect) {
//        if ([[dicSelect valueForJSONStrKey:@"id"] isEqualToString:@"products"]) {
//            weakTable2.hidden=YES;
//            weakTable1.hidden=NO;
//        }else{
//            weakTable1.hidden=YES;
//            weakTable2.hidden=NO;
//        }
        weakSelf.type=[dicSelect valueForJSONStrKey:@"id"];
        [weakSelf request];
    }];
}
#pragma mark  tableview网络请求
-(void)request{
    krequestParam
    
    [dictparam setObject:@"%@" forKey:@"page"];
    [dictparam setObject:@"20" forKey:@"pagesize"];
//    [dictparam setObject:@"products" forKey:@"type"];
    if (_type) {
        [dictparam setObject:_type forKey:@"type"];
    }
    [_mtableView showRefresh:YES LoadMore:YES];
    _mtableView.urlString=[NSString stringWithFormat:@"user/browse%@",dictparam.wgetParamStr];
    [_mtableView refresh];
    
    
}
#pragma mark - 按钮事件监听


#pragma mark - 代理事件



#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(RHTableView *)tableView{
    
    
    return 1;
    
}

-(NSInteger)tableView:(RHTableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    return tableView.dataArray.count;
}

-(CGFloat)tableView:(RHTableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
}
-(CGFloat)tableView:(RHTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([self.type isEqualToString:@"products"]){
    return 105+4.5;
    }else return 94;
}
-(CGFloat)tableView:(RHTableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    return 0.001;
}

-(UIView *)tableView:(RHTableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
    
    
}

#pragma mark  cell处理
- (UITableViewCell *)tableView:(RHTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.type isEqualToString:@"products"]){
        //
        UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            cell.frameWidth=kScreenWidth;
            cell.backgroundColor=zrgbwhiteColor;
            UILabel*lbtitle=[RHMethods lableX:10 Y:16 W:kScreenWidth-20 Height:14 font:14 superview:cell withColor:nil text:@"上海梧桐融资租赁有限公司（简称梧桐公司）"];
            UILabel*lbleft1=[RHMethods lableX:25 Y:lbtitle.frameYH+ 15 W:kScreenWidth/2-25-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"售价：30万  "];
            UILabel*lbleft2=[RHMethods lableX:25 Y:lbleft1.frameYH+10   W:kScreenWidth/2-25-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"成立时间：2017-1-1"];
            UILabel*lbright1=[RHMethods lableX:kScreenWidth/2 Y:lbleft1.frameY W:kScreenWidth/2-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"注册资本：300万"];
            UILabel*lbright2=[RHMethods lableX:kScreenWidth/2 Y:lbleft2.frameY W:kScreenWidth/2-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"发布时间：2017-6-1"];
            UIView*viewlin=[RHMethods viewWithFrame:CGRectMake(0, 104.5, kScreenWidth, 5) backgroundcolor:rgbGray superView:cell];
            tableView.backgroundColor=rgbGray;
            
            
            UIImageView* imageRedV=[RHMethods imageviewWithFrame:CGRectMake(0, 7+2, 10, 10) defaultimage:@"redTip070116" contentMode:UIViewContentModeCenter];
            [cell addSubview:imageRedV];
            
            UIImageView*imgVclock=[RHMethods imageviewWithFrame:CGRectMake(0, 86, 10, 10) defaultimage:@"xkf_timeicon" supView:cell];
            imgVclock.frameRX=66;
            UILabel*lbtime=[RHMethods lableX:imgVclock.frameXW+5 Y:0 W:60 Height:10 font:10 superview:cell withColor:rgb(153,153,153) text:@"2017-02-03"];
            lbtime.centerY=imgVclock.centerY;
            
            
            [cell setAddValue:lbtitle forKey:@"lbtitle"];
            [cell setAddValue:lbleft1 forKey:@"lbleft1"];
            [cell setAddValue:lbleft2 forKey:@"lbleft2"];
            [cell setAddValue:lbright1 forKey:@"lbright1"];
            [cell setAddValue:lbright2 forKey:@"lbright2"];
            [cell setAddValue:viewlin forKey:@"viewlin"];
            [cell setAddValue:imageRedV forKey:@"imageRedV"];
            [cell setAddValue:lbtime forKey:@"lbtime"];
            
            
        }
        UILabel*lbtitle=[cell getAddValueForKey:@"lbtitle"];
        UILabel*lbleft1=[cell getAddValueForKey:@"lbleft1"];
        UILabel*lbleft2=[cell getAddValueForKey:@"lbleft2"];
        UILabel*lbright1=[cell getAddValueForKey:@"lbright1"];
        UILabel*lbright2=[cell getAddValueForKey:@"lbright2"];
        UILabel*imageRedV=[cell getAddValueForKey:@"imageRedV"];
        UILabel*lbtime=[cell getAddValueForKey:@"lbtime"];
        
        NSDictionary *dic=tableView.dataArray[indexPath.row];
        lbtitle.text=[dic objectForJSONKey:@"title"];
        imageRedV.frameX=lbtitle.frameX+[lbtitle.text widthWithFont:14];
        imageRedV.hidden=[[dic valueForJSONStrKey:@"isread"] isEqualToString:@"1"];
        imageRedV.hidden=YES;
        lbleft1.text=[NSString stringWithFormat:@"售价：%@",[dic objectForJSONKey:@"price"]];
        lbright1.text=[NSString stringWithFormat:@"注册资本：%@",[dic objectForJSONKey:@"regcapital"]];
        lbleft2.text=[NSString stringWithFormat:@"成立时间：%@",[dic objectForJSONKey:@"regtime"]];
        lbright2.text=[NSString stringWithFormat:@"发布时间：%@",[dic objectForJSONKey:@"ctime"]];
        lbtime.text=[NSString stringWithFormat:@"%@",[dic objectForJSONKey:@"createtime"]];
        
        return cell;
        
    }else {
    
        NSDictionary *dic=[tableView.dataArray objectAtIndex:indexPath.row];
//        NewsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"NewsTableViewCell" forIndexPath:indexPath];
        
        NewsTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"NewsTableViewCell"];
        
        if (cell == nil) {
            cell = [[NewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsTableViewCell"];
        }
        [cell setValueForDictionary:dic indexPath:indexPath type:@"newslog"];
    
     return cell;
    }
//    return [UITableViewCell new];
}
#pragma mark  选中cell
-(void)tableView:(RHTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic=tableView.dataArray[indexPath.row];
    if([self.type isEqualToString:@"products"]){
        
        [UTILITY.currentViewController pushController:[V4TestLicensePlateContentViewController class] withInfo:@"Insurance" withTitle:@"牌照详情" withOther:dic];
        if ([[dic valueForJSONStrKey:@"isread"] isEqualToString:@"0"]) {
            [dic setValue:@"1" forKey:@"isread"];
            [tableView.dataArray replaceObjectAtIndex:indexPath.row withObject:dic];
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            
        }
    }else{
        [self pushController:[InsuranceBrokersContentViewController class] withInfo:@"new" withTitle:[self.otherInfo valueForJSONStrKey:@"subTitle"] withOther:dic];
        if ([[dic valueForJSONStrKey:@"isred"] isEqualToString:@"0"]) {
            NSMutableDictionary *dicTemp=[NSMutableDictionary dictionaryWithDictionary:dic];
            [dicTemp setValue:@"1" forKey:@"isred"];
            [tableView.dataArray replaceObjectAtIndex:indexPath.row withObject:dicTemp];
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }
    }
    
    
}

- (NSArray *)tableView:(RHTableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 添加一个删除按钮
    
      __weak __typeof(self) weakSelf = self;
       NSDictionary *dic=tableView.dataArray[indexPath.row];
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
//        NSLog(@"点击了删除");
        
        [MyUiIAlertView showAlerttWithTitle:@"是否删除此条记录" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定" WithAlertBlock:^(UIAlertView * _Nonnull alertView, NSInteger buttonIndex) {
            if (buttonIndex==1) {
                krequestParam
                if (weakSelf.type) {
                    [dictparam setObject:_type forKey:@"type"];
                }
                [dictparam setObject:[dic objectForJSONKey:@"browseid"] forKey:@"id"];
                [NetEngine createPostAction:@"user/delbrowse" withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
                    if ([[resData getSafeObjWithkey:@"status"] isEqualToString:@"200"]) {
                        [tableView.dataArray removeObject:dic];
                        [tableView reloadData];
                        [SVProgressHUD showSuccessWithStatus:[resData valueForJSONKey:@"info"]];
                        //            [self addView];
                        
                    }else{
                        [SVProgressHUD showErrorWithStatus:[resData valueForJSONKey:@"info"]];
                        
                    }
                }];

            }
        }];
    
    }];
    
    return @[deleteRowAction];
}

@end
